package org.darkforest.admin.init;

import org.darkforest.admin.core.ServerDataContext;
import org.darkforest.common.alipay.config.AlipayConfig;
import org.darkforest.common.services.SettingService;
import org.darkforest.common.util.AliOssFileUploadUtil;
import org.darkforest.common.util.SystemConstant;
import org.darkforest.enums.SettingNameEnum;
import org.darkforest.vo.SiteSettingInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;

/**
 * Created by valentina on 15/7/22.
 */
@Service
public class SystemConstantInit implements ServletContextAware {

    Logger logger = LoggerFactory.getLogger(SystemConstantInit.class);

    @Autowired
    private SettingService settingService;

    @Override
    public void setServletContext(ServletContext application) {
        ServerDataContext serverDataContext = new ServerDataContext();
        serverDataContext.init(application);
        //设置图片路径
        AliOssFileUploadUtil.bucketName = settingService.getSettingInfo(SettingNameEnum.ALIOSS_BUCKETNAME);
        AliOssFileUploadUtil.accessId = settingService.getSettingInfo(SettingNameEnum.ALIOSS_ACCESS_ID);
        AliOssFileUploadUtil.accessKey = settingService.getSettingInfo(SettingNameEnum.ALIOSS_ACCESS_KEY);
//        AliOssFileUploadUtil.endPoint = settingService.getSettingInfo(SettingNameEnum.ALIOSS_ENDPOINT);
        AliOssFileUploadUtil.endPoint = settingService.getSettingInfo(SettingNameEnum.IMAGE_UPLOAD_ENDPOINT);
//        SystemConstant.ALIOSS_PATH = "http://" + AliOssFileUploadUtil.bucketName + ".oss-cn-beijing.aliyuncs.com";
        SystemConstant.ALIOSS_PATH = settingService.getSettingInfo(SettingNameEnum.IMAGE_SHOW_ENDPOINT);
        SystemConstant.PCWEB_PATH = settingService.getSettingInfo(SettingNameEnum.PCWEBSITE);
        SystemConstant.WX_WEB_PATH = settingService.getSettingInfo(SettingNameEnum.WXWEBSITE);

        SystemConstant.PCWEB_GOODSCOMM_DETAIL_PATH= SystemConstant.PCWEB_PATH + "/goods/detailByComm?commId=";
        application.setAttribute("alioss_path", SystemConstant.ALIOSS_PATH);
        application.setAttribute("wxweb_path", SystemConstant.WX_WEB_PATH);

        ////设置支付宝参数
        String domain_name = settingService.getSettingInfo(SettingNameEnum.DOMAIN_NAME);
        AlipayConfig.partner = settingService.getSettingInfo(SettingNameEnum.ALIPAY_PARTNER);
        AlipayConfig.seller_email = settingService.getSettingInfo(SettingNameEnum.ALIPAY_ACCOUNT);
        AlipayConfig.key = settingService.getSettingInfo(SettingNameEnum.ALIPAY_KEY);
        AlipayConfig.notify_url = domain_name + AlipayConfig.notify_url;
        AlipayConfig.refundNotifyUrl = domain_name + application.getContextPath()+ AlipayConfig.refundNotifyUrl;
        AlipayConfig.return_url = domain_name + AlipayConfig.return_url;
        AlipayConfig.show_url = domain_name + AlipayConfig.show_url;
        SiteSettingInfo siteSettingInfo = new SiteSettingInfo();
        //读取数据库配置加载到对象
        settingService.initSiteSetting(siteSettingInfo);
    }
}
